package com.tzq.autoticket;

import com.tzq.autoticket.bean.Tourist;
import com.tzq.autoticket.utils.Log;
import com.tzq.autoticket.utils.TUtils;
import com.tzq.autoticket.utils.XlsUtils;
import net.bytebuddy.implementation.bytecode.Throw;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Config {
    private static Config sInstance;

    private final static String TOURIST_FILE_NAME = "touristinfo.xls";
    private final static String TOURIST_SUCCESS_FILE_NAME = "success.xls";
    private final static String CONFIG_FILE_NAME = "cfg.txt";
    private final static String DRIVER_NAME = "chromedriver.exe";
    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private String userName, password, dxNumber, dxCountry, dxProvince, sDate;
    private long sleep;
    private Calendar calendar;
    private List<Tourist> touristList = new ArrayList<>();
    private List<Tourist> touristSuccessList = new ArrayList<>();

    public static Config getInstance() {
        if (sInstance == null) {
            try {
                sInstance = new Config();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.info("sInstance=" + sInstance);
        }
        return sInstance;
    }

    public Config() throws Exception {
        calendar = Calendar.getInstance();
        if (Main.DEBUG) {
//                userName = "gansuyouyou";
//                password = "chenyan8888";
            userName = "mingzhilv";
            password = "he13980783600";
            sDate = "2018-05-07";
            calendar.setTime(sdf.parse(sDate));
            dxNumber = "15987563214";
            dxCountry = "中国";
            dxProvince = "四川省";
            sleep = 500;
            touristList = XlsUtils.readTourist("D:\\" + TOURIST_FILE_NAME);
        } else {
            Properties properties = new Properties();
            String projectDirPath = TUtils.getProjectDirPath();
            Log.info("projectDirPath=" + projectDirPath);
            properties.load(new InputStreamReader(new FileInputStream(projectDirPath + File.separator + CONFIG_FILE_NAME), "utf-8"));
            for (String s : properties.stringPropertyNames()) {
                Log.info("---" + s);
            }
            userName = properties.getProperty("userName").trim();
            password = properties.getProperty("password").trim();
            dxNumber = properties.getProperty("dxNumber").trim();
            dxCountry = properties.getProperty("dxCountry").trim();
            dxProvince = properties.getProperty("dxProvince").trim();
            sDate = properties.getProperty("date").trim();
            sleep = Long.valueOf(properties.getProperty("sleep").trim());

            Date date = sdf.parse(sDate);
            if (date.getTime() < System.currentTimeMillis()) {
                throw new Exception("date is not correct!");
            }
            calendar.setTime(sdf.parse(sDate));
            touristList = XlsUtils.readTourist(new File(projectDirPath) + File.separator + TOURIST_FILE_NAME);
        }
    }

    public String getUserName() {
        return userName;
    }


    public String getPassword() {
        return password;
    }


    public Calendar getCalendar() {
        return calendar;
    }

    public static String getDriverPath() {
        if (Main.DEBUG) {
            return "D:\\work\\autoticket\\me\\resources\\chromedriver.exe";
        }
        return new File(TUtils.getProjectDirPath()) + File.separator + "lib" + File.separator + DRIVER_NAME;
    }

    public String getTmpXlsPath() {
        if (Main.DEBUG) {
            return "D:\\work\\autoticket\\me\\resources\\" + TOURIST_FILE_NAME;
        } else {
            return TUtils.getProjectDirPath() + File.separator + "lib" + File.separator + TOURIST_FILE_NAME;
        }
    }

    public String getSuccessXlsPath() {
        if (Main.DEBUG) {
            return "D:\\work\\" + TOURIST_SUCCESS_FILE_NAME;
        } else {
            return TUtils.getProjectDirPath() + File.separator + TOURIST_SUCCESS_FILE_NAME;
        }
    }

    public String getSuccessAudioPath() {
        if (Main.DEBUG) {
            return "D:\\Download\\success.mp3";
        } else {
            return TUtils.getProjectDirPath() + File.separator + "lib" + File.separator + "success.mp3";
        }
    }

    public List<Tourist> getTouristList() {
        return touristList;
    }


    public long getSleep() {
        return sleep;
    }


    public String getDxNumber() {
        return dxNumber;
    }


    public String getsDate() {
        return sDate;
    }


    public String getDxCountry() {
        return dxCountry;
    }


    public String getDxProvince() {
        return dxProvince;
    }


    public List<Tourist> getTouristSuccessList() {
        return touristSuccessList;
    }

    @Override
    public String toString() {
        return "Config{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", dxNumber='" + dxNumber + '\'' +
                ", dxCountry='" + dxCountry + '\'' +
                ", dxProvince='" + dxProvince + '\'' +
                ", sDate='" + sDate + '\'' +
                ", sleep=" + sleep +
                '}';
    }
}
